package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageInfo;
import cn.itsource.pethome.org.domain.DictionaryType;
import cn.itsource.pethome.org.query.DictionaryTypeQuery;
import cn.itsource.pethome.org.service.IDictionaryTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController//@Controller+@ResponseBody
@RequestMapping("/dictionary")
public class DictionaryTypeController {
    @Autowired(required = false)
    IDictionaryTypeService typeService;

    //查询所有数据类型
    @PatchMapping//批量操作数据
    //RequestBody注释了才能接收到前台穿过来的数据
    public PageInfo findPageByQuery(@RequestBody DictionaryTypeQuery dictionaryTypeQuery) {
        return typeService.findPageByQuery(dictionaryTypeQuery);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "数据字典删除",notes = "根据id删除相关数据")
    //@PathVariable 是从地址栏获取到id
    public AjaxResult delete(@PathVariable("id") Long id){

        try {
            typeService.delete(id);
            return new AjaxResult(true,"删除成功拉~");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @PostMapping//组合注解，是post请求+路径
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult save(@RequestBody DictionaryType dictionaryType){
        try {
            typeService.save(dictionaryType);
            return new AjaxResult(true,"操作成功~");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败！");
        }
    }
    //获取到所有的类型
    @PatchMapping("/getTypes")
    public List<DictionaryType> findTypes() {
        return typeService.findAll();
    }
    

}
